<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

if ($op=="toggle") {
	$idlist = util::stringToArray(stripslashes($param['id']),":");	
	foreach ($idlist as $id) {
		$upload = new upload;
		$upload->id=$id;
		$r = $loader->get($upload);
		$fail = 0;
		if ($r) {
			if ($upload->statusid==UPLOAD_OK || $upload->statusid==UPLOAD_FAIL) {			
				if ($upload->takeuser==null) {
					$upload->takeuser=$CURRENT_USER->userid;
					$upload->takedate=util::now();
					$loader->execute($upload->updateFlag());
				} else {
					if ($upload->takeuser == $CURRENT_USER->userid) {
						$upload->takeuser=null;
						$upload->takedate=null;
						$loader->execute($upload->updateFlag());				
					} else {
						$fail++;
					}
				}
			} else {
				$fail++;
			}
		}
	}
	if ($fail>0) {
		failed(s("Some files cannot be modified right now."));
	} else {
		success();	
	}
} else if ($op=="ok" || $op=="nok") {
	$idlist = util::stringToArray(stripslashes($param['id']),":");
	$cmt = $param['comment'];
	if (strlen($param['comment'])>255) {
		$cmt = substr($param['comment'],0,255);
	}
	foreach ($idlist as $id) {
		$upload = new upload;
		$upload->id=$id;
		$r = $loader->get($upload);
		$fail=0;
		$hact = HIS_ACT_CMM;
		if ($r) {
			if ($upload->statusid==UPLOAD_OK || $upload->statusid==UPLOAD_FAIL) {
				if ($upload->takeuser==$CURRENT_USER->userid or $upload->takeuser==null or $upload->takeuser=="" ) {
					$uplist = array();
					$upd = new upload_data;
					$upd->objid=$upload->id;
					$list = $loader->loadListBy($upd,$upd->listOK());
					foreach ($list as $ud) {
						$file = new files($upload->projectid);
						$file->id=$ud->fileid;
						$r = $loader->get($file);
						if ($r) {
							if ($op=="ok") {
								switch ($ud->filestatus) {
									case FILE_STATUS_TRANS:
										$file->statusid = FILE_STATUS_CHKPND;
										break;
									case FILE_STATUS_PROOF:
										$file->statusid = FILE_STATUS_DONE;
										break;
									default:
										$file->statusid = FILE_STATUS_CHKPND;
										break;
								}
								//$file->comment=String::format(s("Submitted by {0}"),$CURRENT_USER->userid);
								if ($cmt=="") {
									$cmt = $file->comment;
								}
								$file->comment = $cmt;						
								$loader->update($file);
							} else {
								switch ($ud->filestatus) {
									case FILE_STATUS_TRANS:
										$file->statusid = FILE_STATUS_LOCKED;
										break;
									case FILE_STATUS_PROOF:
										$file->statusid = FILE_STATUS_CHKPND;
										break;
									default:
										$file->statusid = FILE_STATUS_LOCKED;
										break;
								}
								//$file->comment=String::format(s("Rejected by {0}"),$CURRENT_USER->userid);
								$hact=HIS_ACT_REJ;
								$file->comment = $cmt;
								$loader->update($file);
							}
							$loader->execute(history::create($file,null,$hact));
							array_push($uplist,$file);
						}
						
					}
					$loader->delete($upload);
					$loader->delete($upd);
					@unlink(UPLOAD_PATH."/".$upload->id);
	
					mailUploadDone($CURRENT_USER,$uplist,$op,$upload->userid,$cmt);
				} else {
					$_log->warn("File $upload->id is locked");
					$fail++;
				}
			} else {
				$_log->warn("File $upload->id is submitted");
				$fail++;				
			}
		}
	}
	if ($fail>0) {
		failed(s("Some files cannot be modified right now."));
	} else {
		success();	
	}
} else if ($op == "svn") {

	$idlist = util::stringToArray(stripslashes($param['id']),":");	
	foreach ($idlist as $id) {
		$upload = new upload;
		$upload->id=$id;
		$r = $loader->get($upload);
		$fail=0;
		$fail2=0;
		if ($r) {
			if ($upload->statusid==UPLOAD_OK || $upload->statusid==UPLOAD_FAIL) {
				if ($upload->takeuser==$CURRENT_USER->userid or $upload->takeuser==null or $upload->takeuser=="" ) {
					$job = new job;
					$job->statusid=JOB_STATUS_NEW;
					$job->type=JOB_TYPE_UPSVN;
					$job->runhash=$upload->id;
					$job->teamid=getTeamId($CURRENT_USER);
					$cmt = $param['comment'];
					if (strlen($param['comment'])>100) {
						$cmt = substr($param['comment'],0,100);
					}
					$job->filler=$cmt;
					$job->extra=util::hide($param['uid']." ".$param['psd']);
					$job->userid=$CURRENT_USER->userid;
					$loader->save($job);
	
					$upload->statusid = UPLOAD_SEND;
					$upload->takeuser=$CURRENT_USER->userid;
					$upload->takedate=util::now();
					$loader->execute($upload->updateAll());
				} else {
					$_log->warn("File $upload->id is locked");
					$fail++;
				}				
			} else {
				$_log->warn("File $upload->id is submitted");
				$fail2++;
			}

		}
	}
	if ($fail>0) {
		failed(s("Some files cannot be modified right now."));
	} else if ($fail2>0) {
		failed(s("Some files are already submitted."));
	} else {
		//success(s("Files submitted."));
		success();	
	}
	
} else {
	$upload = new upload;
	if ($CURRENT_USER->groupid==GROUP_ADMIN) {
		$list = $loader->loadList($upload);
	} else {
		$list = $loader->loadListBy($upload,$upload->listByTeam(getTeamId($CURRENT_USER)));
	}
	
	$rlist = array();
	foreach ($list as $obj) {
		if ($param['all']=="true" || !$obj->isTaked() || $obj->takeuser == $CURRENT_USER->userid) {
			$upd = new upload_data;
			$upd->objid = $obj->id;
			$ulist = $loader->loadListBy($upd,$upd->listOK());
			$obj->data= "[".jsonEncodeList($ulist,array("id","name","path"))."]";
			array_push($rlist,$obj);
		}
	}
	
	out(jsonList($rlist,array("id","userid","udate","statusid","teamid","projectid","takeuser","takedate","&data")));
}
	
?>
